<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" size="small" :inline="true" label-width="68px">
      <el-form-item label="合同名称" prop="e_hname">
        <el-input v-model="queryParams.e_hname" placeholder="请输入合同名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="供应商名称" label-width="100px" prop="h_name">
        <el-input v-model="queryParams.h_name" placeholder="请输入供应商名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="风险项" prop="rm_item">
        <el-select v-model="queryParams.rm_item" placeholder="请选择风险项" @keyup.enter.native="handleQuery">
          <el-option v-for="dict in rm_items" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="风险处理状态" prop="rm_status" label-width="110px">
        <el-select v-model="queryParams.rm_status" placeholder="请选择风险处理状态" @keyup.enter.native="handleQuery">
          <el-option v-for="dict in rm_Statu" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
           type="primary"
           plain
           icon="el-icon-plus"
           size="mini"
           @click="handleAdd"
           v-hasRole="['common','controller']">
          新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" stripe :data="monitorList">
      <el-table-column type="index" label="序号" align="center"/>
      <el-table-column label="合同编号" align="center" prop="csContract.e_hcode"/>
      <el-table-column label="合同名称" align="center" prop="csContract.e_hname"/>
      <el-table-column label="供应商名称" align="center" prop="csSupplier.h_name"/>
      <el-table-column label="风险说明" align="center" prop="rm_illustrate"/>
      <el-table-column label="发生时间" align="center" prop="rm_happen_date">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.rm_happen_date, '{y}-{m}-{d}') }}</span>
          </template>
      </el-table-column>
      <el-table-column label="风险项" align="center" prop="rm_item">
        <template slot-scope="scope">
          <el-tag size="small" v-if="monitorList[scope.$index].rm_item==1" type="danger">合同履约点逾期风险</el-tag>
          <el-tag size="small" v-if="monitorList[scope.$index].rm_item==2" type="danger">合同变更风险</el-tag>
          <el-tag size="small" v-if="monitorList[scope.$index].rm_item==3" type="danger">收款时间点逾期风险</el-tag>
          <el-tag size="small" v-if="monitorList[scope.$index].rm_item==4" type="danger">故意逃避监控风险</el-tag>
          <el-tag size="small" v-if="monitorList[scope.$index].rm_item==5" type="danger">外部签约方异常风险</el-tag>
          <el-tag size="small" v-if="monitorList[scope.$index].rm_item==6" type="danger">合同履约率风险</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="风险处理状态" align="center" prop="rm_status">
        <template slot-scope="scope">
          <el-tag size="small" v-if="monitorList[scope.$index].rm_status==1" type="danger">未处理</el-tag>
          <el-tag size="small" v-if="monitorList[scope.$index].rm_status==2" type="success">已处理</el-tag>
          <el-tag size="small" v-if="monitorList[scope.$index].rm_status==3" type="info">已忽略</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="small" type="primary" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasRole="['common','controller']">修改</el-button>
          <router-link :to="'riskHandling?rm_id='+scope.row.rm_id">
            <el-button size="small" icon="el-icon-document-checked" style="margin-left: 10px;" v-hasRole="['controller']">
              风险处理
            </el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog width="30%" :title="title" :visible.sync="open" append-to-body>
      <el-dialog
        title="选择合同"
        width="50%"
        :visible.sync="innerVisible"
        append-to-body @close="innerVisible = false">
        <el-table
          ref="singleTable"
          :data="contractData"
          highlight-current-row
          @current-change="handleCurrentChange"
          style="width: 100%">
          <el-table-column
            type="index"
            width="60">
          </el-table-column>
          <el-table-column
            property="e_hcode"
            label="合同编号"
            width="185">
          </el-table-column>
          <el-table-column
            property="e_hname"
            label="合同名称"
            width="185">
          </el-table-column>
          <el-table-column
            property="csSupplier.h_name"
            label="供应商名称">
          </el-table-column>
        </el-table>
        <pagination
          v-show="total1>0"
          :total="total1"
          :page.sync="contract.pageNum"
          :limit.sync="contract.pageSize"
          @pagination="innerShow"
        />
      </el-dialog>


      <el-form label-position="80px" ref="form" :model="form" :rules="rules">
        <el-form-item label="合同名称" prop="e_hname">
          <el-input placeholder="请选择合同" v-model="contractName" @focus="innerShow"/>
        </el-form-item>
        <el-form-item label="风险说明" prop="rm_illustrate">
          <el-input type="textarea" placeholder="请选择风险说明" v-model="form.rm_illustrate"/>
        </el-form-item>
        <el-form-item label="发生日期" prop="rm_happen_date">
          <el-date-picker v-model="form.rm_happen_date" type="date" placeholder="请选择发生日期" value-format="yyyy-MM-dd" clearable>

          </el-date-picker>
        </el-form-item>
        <el-form-item label="风险项" prop="rm_item">
          <el-select placeholder="请选择发生日期" v-model="form.rm_item">
              <el-option v-for="dict in rm_items" :key="dict.dictValue" :label="dict.dictLabel" :value="Number(dict.dictValue)"></el-option>
          </el-select>
          <!-- <el-input placeholder="请选择风险说明" v-model="form.rm_item"/> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listRiskMonitor,getRiskMonitor,addRiskMonitor,updateRiskMonitor,delRiskMonitor} from "@/api/system/riskMonitor";
import {listContract} from "@/api/system/contract";
import {selectDictLabel,selectDictLabels} from "@/utils/ruoyi";

export default{
  name:"Monitor",
  data(){
    return{
      contract:{
        e_cancel: 0,
        o_hstatus: 3,
        pageNum: 1,
        pageSize: 10
      },
      //合同总数
      total1: 0,
      //合同数据
      contractData:[],
      //内嵌对话框状态
      innerVisible: false,
      //遮罩层
      loading: true,
      //显示搜索条件
      showSearch: true,
      //总条数
      total: 0,
      //供应商风险监控表格数据
      monitorList: [],
      //风险项字典数据
      rm_items: [],
      //风险处理状态字典数据
      rm_Statu: [],
      //弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //查询参数
      queryParams:{
        pageNum: 1,
        pageSize: 10,
        e_hname: null,
        h_name: null,
        rm_item: null,
        rm_status: null
      },
      //表单参数
      form: {},
      //表单验证
      rules: {},
      contractName: null
    };
  },
  created(){
    if(sessionStorage.getItem('a1')!=null && sessionStorage.getItem('a2')){
      this.queryParams.pageNum=JSON.parse(sessionStorage.getItem('a1'))
      this.queryParams.pageSize=JSON.parse(sessionStorage.getItem('a2'))
      sessionStorage.removeItem('a1')
      sessionStorage.removeItem('a2')
    }
    this.getList();
    //查询风险项字典
    this.getDicts("bs_risk_monitoring_item").then(res=>{
      this.rm_items=res.data;
    })
    //查询风险处理状态字典
    this.getDicts("bs_risk_monitoring_status").then(res=>{
      this.rm_Statu=res.data;
    })
  },
  beforeRouteLeave(to,from,next){
    sessionStorage.setItem('a1',this.queryParams.pageNum)
    sessionStorage.setItem('a2',this.queryParams.pageSize)
    next(true)
  },
  methods:{
    getList(){
      this.loading=true;
      console.log(this.queryParams,"555");
      listRiskMonitor(this.queryParams).then(response=>{
        console.log(response,"DSADSDS")
        this.monitorList=response.rows;

        this.total=response.total;
        this.loading=false;
      });
    },
    //取消按钮
  cancel(){
    this.open=false;
    this.reset();
  },
  handleCurrentChange(val){
    console.log(val)
    this.contractName=val.e_hname
    this.form.eid=val.eid
    this.innerVisible=false
  },
  //表单重置
  reset(){
    this.form={
      rm_id:null,
      eid:null,
      rm_illustrate:null,
      rm_happen_date:null,
      rm_item:null,
      rm_status:1,
    }
    this.resetForm("form");
  },
  innerShow(){

    console.log(this.contract,"wqerdgtfhyjuk")
    listContract(this.contract).then(res=>{
      console.log(res,"D555555558848")
      this.contractData=res.rows;
      this.total1=res.total;
    });
    this.innerVisible=true
  },
  //搜索按钮操作
  handleQuery(){
    this.queryParams.pageNum=1;
    this.getList();
  },
  //重置按钮操作
  resetQuery(){
    this.resetForm("queryForm");
    this.handleQuery();
  },
  //新增按钮操作
  handleAdd(){
    console.log("DSDSD8888888888")
    this.contractName=null
    this.reset()
    this.open=true;
    this.title="添加供应商风险监控";
  },
  //修改按钮操作
  handleUpdate(row){

    this.reset();
    const rm_id=row.rm_id
    getRiskMonitor(rm_id).then(response=>{
      console.log(response)
      this.form=response.data;
      this.contractName=row.csContract.e_hname;
      this.open=true;
      this.title="修改供应商风险监控";
    })
  },
  submitForm(){
    this.$refs["form"].validate(valid=>{
      if(valid){
        if(this.contractName==null||this.contractName==''||this.form.rm_illustrate==null||this.form.rm_illustrate==''||this.form.rm_happen_date==null||this.form.rm_happen_date==''||this.form.rm_item==null||this.form.rm_item==''){
          this.$message.error("信息不能为空")
        }else{
          if(this.form.rm_id!=null){
            updateRiskMonitor(this.form).then(response=>{
              this.$modal.msgSuccess("修改成功");
              this.open=false;
              this.getList();
            });
          }else{
            addRiskMonitor(this.form).then(response=>{
              this.$modal.msgSuccess("新增成功");
              this.open=false;
              this.getList();
            })
          }
        }
      }
    })
  }
  }

}
</script>
